package com.iot.ic.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DatetimeUtil {

	public static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
	
	/**
	 * 获取年份
	 * @return
	 */
	public static int getCurrentYear(){
		Calendar ca = Calendar.getInstance();
	    return ca.get(Calendar.YEAR);
	}
	
	/**
	 * 获取月份 
	 * @return
	 */
	public static int getCurrentMonth(){
		Calendar ca = Calendar.getInstance();
		return ca.get(Calendar.MONTH);
	}
	
	/**
	 * 获取日
	 * @return
	 */
	public static int getCurrentDay(){
		Calendar ca = Calendar.getInstance();
	    return ca.get(Calendar.DATE);
	}

	/**
	 * 获取当天零点的时间戳
	 * @return
     */
	public static long getTodayStamp(){
		Calendar ca = Calendar.getInstance();
		ca.set(getCurrentYear(),getCurrentMonth(),getCurrentDay());
		return  ca.getTimeInMillis();
	}
	
	/**
	 * 获取当前日期前后monthdiff个月份的日期的年份
	 * @param monthdiff 差距的月份， 日期之前的月份用负数
	 * @return
	 */
	public static int GetdiffYear(int monthdiff){
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, monthdiff);
		return ca.get(Calendar.YEAR);
	}
	
	/**
	 * 获取当前日期前后monthdiff个月份的日期的月份
	 * @param monthdiff 差距的月份， 日期之前的月份用负数
	 * @return
	 */
	public static int GetdiffMonth(int monthdiff){
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, monthdiff);
		return ca.get(Calendar.MONTH);
	}
	
	/**
	 * 获取月份 
	 * @return
	 */
	public static int getCurrentWeekDay(){
		Calendar ca = Calendar.getInstance();
		return ca.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @param format
     * @return
     */
    public static String timeStampToDate(Long seconds,String format) {  
        if(seconds == null || seconds==0){  
            return "";  
        }  
        if(format == null || format.isEmpty()) 
        	format = "yyyy-MM-dd HH:mm:ss";  
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(new Date(Long.valueOf(seconds+"000")));  
    }
    
    /**
     * Date时间转时间字符串
     * @param date
     * @param format
     * @return
     */
    public static String DateToString(Date date,String format){
        if(format == null || format.isEmpty()) 
        	format = "yyyy-MM-dd HH:mm:ss";  
    	SimpleDateFormat sdf=new SimpleDateFormat(format); 
    	String str=sdf.format(date);
    	return str;
    }

    /**
	 * Date时间转时间字符串
	 * @param dateStr
	 * @param format
	 * @return
     */
    public static Date stringToDate(String dateStr,String format){
    	if(format == null || format.isEmpty()) 
        	format = "yyyy-MM-dd HH:mm:ss"; 
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	Date date;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			return new Date();
		}
    	return date;
    }

    /**
	 * 日期格式字符串转换成时间戳
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @param date_str 字符串日期
	 * @return
     */
    public static long dateToTimeStamp(String date_str,String format){  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return (long)(sdf.parse(date_str).getTime()/1000);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return 0;  
    }  
      
    /** 
     * 取得当前时间戳（精确到秒） 
     * @return 
     */  
    public static String timeStamp(){  
        long time = System.currentTimeMillis();  
        String t = String.valueOf(time/1000);  
        return t;  
    }  
    
    /**
     * 获取两个时间戳之间的时间长度字符串表示
     * @param start
     * @param end
     * @return
     */
    public static String getTimeLong(Long start,Long end){
    	if (end==null||end==0||start==null) {
			return "";
		}
        long l=end-start;
        long day=l/(24*60*60);
        long hour=(l/(60*60)-day*24);
        long min=((l/(60))-day*24*60-hour*60);
        long s=(l-day*24*60*60-hour*60*60-min*60);
        String time="";
        if (day!=0) {
			time+=day+"天"+hour+"小时";
		}else if (hour!=0) {
        	time+=hour+"小时";
		}else if (min!=0){
			time+=min+"分";
		}
        return time+s+"秒";
    }
    
    /**
     * 获取某年份距今的年数
     * @param year
     * @return
     */
    public static double getYearsFromNow(Integer year){
    	//1468831193775
    	//1275321600000
    	if (year==null) {
			return 0;
		}
        try {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			long timeStamp=sdf.parse(year+"-06-01").getTime();
			long tmp=(System.currentTimeMillis()-timeStamp);
			double result=tmp/1000/60/60/24/365;
			return result;
		} catch (ParseException e) {
	        return 0;
		}
    }
    
    /**
     * 获取当天零点的时间戳
     * @return
     */
    public static long getTodayZero(){
    	long current = System.currentTimeMillis();// 当前时间毫秒数
		long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();// 今天零点零分零秒的毫秒数
		return zero;
    }
    
    /**
	 * * 获取当前时间点前指定一段时间的整点数组或日期数组
	 * 
	 * @param dateStyle
	 *            数据类型 day或hour
	 * @param dates
	 *            小时数或天数
	 * @return
	 */
	public static String[] getDateArry(String dateStyle, int dates) {
		String time[] = new String[dates];
		DateFormat dateFormat = new SimpleDateFormat("HH");
		int filed = Calendar.HOUR;
		if (dateStyle.equals("hour")) {
			dateFormat = new SimpleDateFormat("HH");
			filed = Calendar.HOUR;
		} else if (dateStyle.equals("day")) {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			filed = Calendar.DAY_OF_YEAR;
		}
		Date date = new Date();
		Calendar rightNow = Calendar.getInstance();
		for (int i = 0; i < dates; i++) {
			rightNow.setTime(date);
			rightNow.add(filed, -i);
			Date dt = rightNow.getTime();
			time[dates - i - 1] = dateFormat.format(dt);
		}
		return time;
	}
	
	/**
	 * * 获取当前时间点前指定一段时间的整点时间
	 * 
	 * @param dateStyle
	 *            数据类型 day或hour
	 * @param dates
	 *            时间间隔
	 * @return
	 */

	public static String getStartDate(String dateStyle, int dates) {
		String startDate = null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
		Date date = new Date();
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		if (dateStyle.equals("hour")) {
			rightNow.add(Calendar.HOUR, -dates);
			Date dt = rightNow.getTime();
			startDate = dateFormat.format(dt);
		} else {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			rightNow.add(Calendar.DAY_OF_YEAR, -dates);
			Date dt = rightNow.getTime();
			startDate = dateFormat.format(dt);
		}
		return startDate;
	}
	
	/**
	 * 获取连续日期列表
	 * 
	 * @param format
	 *            日期格式
	 * @param beginDate
	 * @param endDate
	 * @param step
	 *            间隔天数
	 * @return
	 */
	public static List<String> getJulianDays(String format, String beginDate, String endDate, int step) {
		List<String> dateList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		Date start = null;
		int end = 0;
		try {
			start = sdf.parse(beginDate);
			end = Integer.parseInt(sdf2.format(sdf.parse(endDate)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTime(start);
		while (Integer.parseInt(sdf2.format(cal.getTime())) <= end) {
			SimpleDateFormat sdf3 = new SimpleDateFormat(format);
			dateList.add(sdf3.format(cal.getTime()));
			cal.add(Calendar.DATE, step);
		}
		return dateList;
	}

	/**
	 * 获取一个起止时间的第几周列表
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List getWeekList(long start, long end) {
		List<String> weekList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		Date startDate = new Date(start * 1000);
		cal.setTime(startDate);
		cal.add(Calendar.WEEK_OF_YEAR, -1);
		int year = cal.get(Calendar.YEAR);
		int week = cal.get(Calendar.WEEK_OF_YEAR);
		weekList.add(year + "年第" + week + "周");
		int lastYear = 0;
		int lastWeek = 0;
		while (cal.getTime().getTime() <= end * 1000) {
			cal.add(Calendar.WEEK_OF_YEAR, 1);
			year = cal.get(Calendar.YEAR);
			week = cal.get(Calendar.WEEK_OF_YEAR);
			if ((weekList.size() > 0) && (year == lastYear) && (week < lastWeek)) {
				year++;
			}
			weekList.add(year + "年第" + week + "周");
			lastWeek = week;
			lastYear = year;
		}
		return weekList;
	}

	/**
	 * 获取一个起止时间的第几月列表
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List getMonthList(long start, long end) {
		List<String> monthList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		Date startDate = new Date(start * 1000);
		cal.setTime(startDate);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		if (month+1 >= 10) {
			monthList.add(year + "-" + (month+1));
		} else {
			monthList.add(year + "-0" + (month+1));
		}
		//month的值为（0-11），所以实际月份应该加1
		cal.set(year, month, 1, 0, 0, 0);
		cal.add(Calendar.MONTH, 1);
		while (cal.getTime().getTime() <= end * 1000) {
			year = cal.get(Calendar.YEAR);
			month = cal.get(Calendar.MONTH);
			if (month+1 >= 10) {
				monthList.add(year + "-" + (month+1));
			} else {
				monthList.add(year + "-0" + (month+1));
			}
			cal.add(Calendar.MONTH, 1);
		}
		return monthList;
	}
	
	/**
	 * 获取一个起止时间的第几年列表
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List getYearList(long start, long end) {
		List<String> yearList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		Date startDate = new Date(start * 1000);
		cal.setTime(startDate);
		int year = cal.get(Calendar.YEAR);
		yearList.add(year + "年");
		cal.set(year+1, 0, 0, 0, 0, 0);
		while (cal.getTime().getTime() <= end * 1000) {
			year = cal.get(Calendar.YEAR);
			yearList.add(year + "年");
			cal.add(Calendar.MONTH, 1);
		}
		return yearList;
	}


    /**
	 * 获取两个时间戳之间的时间长度字符串表示
	 * @param l
	 * @return
     */
    public static String getTimeLong(long l){
    	l=l/1000;
        long day=l/(24*60*60);
        long hour=(l/(60*60)-day*24);
        long min=((l/(60))-day*24*60-hour*60);
        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
        String time="";
        if (day!=0) {
			time+=day+"天"+hour+"小时";
		}else if (hour!=0) {
        	time+=hour+"小时";
		}
        return time+min+"分";
    }
    
    /**
     * 获取过去时间和当前时间之间的时间长度字符串表示
     * @param start
     * @param end
     * @return
     */
    public static String getTimeUntilNow(Date date){
    	long l=new Date().getTime()-date.getTime();
    	l=l/1000;
        long day=l/(24*60*60);
        long hour=(l/(60*60)-day*24);
        long min=((l/(60))-day*24*60-hour*60);
        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
        String time="";
        if (day!=0) {
			time+=day+"天"+hour+"小时";
		}else if (hour!=0) {
        	time+=hour+"小时";
		}
        return time+min+"分钟";
    }
      
}
